﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Modules;
using YT.Commons;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTreatmentEffectTitle_Hurry_Backup : Form
    {
       
        private string mstrSickRecID = string.Empty;
        private FormStatus mfrmStatus;
        private string mstrTableName = "TreatmentEffectResult_Emergency";
        private string mstrSeqNo = string.Empty;
        ArrayList marrlist = new ArrayList();
        int mintCount = 5;  // 把菜单的项中的固定5次[疗效评估]的SEQNO排除。
        public frmMDTreatmentEffectTitle_Hurry_Backup()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            mstrSeqNo = SysVar.CurrentMenuInfo.iParameter.ToString();  
        }
        public frmMDTreatmentEffectTitle_Hurry_Backup(string strSickRecID, FormStatus frmStauts)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            DBAccess dbcon =new DBAccess (false,false );
            string strSeqNo= DataFunctions.GetFieldValue ( dbcon,"TreatmentEffectHdr_Emergency"," Max(SeqNo) as SeqNo "," SickRecID='"+ mstrSickRecID +"' and Type='"+ TitleType.MeniereDisease +"' ");           
            mstrSeqNo =( CvtFunc.ToInt ( strSeqNo )+1).ToString ();
            dbcon.CloseCon();
        }

        public frmMDTreatmentEffectTitle_Hurry_Backup(string strSickRecID, FormStatus frmStauts, string strExamSeqNo)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strExamSeqNo;
        }

        private void frmMDTreatmentEffectTitle_Load(object sender, EventArgs e)
        {
            this.Text = "疗效评估";
            FillData();
            timer1.Start();
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "TextBox") con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }
        }
        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, true );
        }

        private void SetControl()
        {
            string strSQL = "select * from TreatmentEffectHdr_Emergency where SeqNo='" + mstrSeqNo + "' and Type='" + TitleType.MeniereDisease + "' and SickRecID='" + mstrSickRecID + "'";
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                panel2.Enabled = false;
            }
            dbcon.CloseCon();
        }

        private void FillData()
        {
            string strTableName = mstrTableName ;
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            SetControl();
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.MeniereDisease.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='" + mstrSeqNo + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.MeniereDisease + "'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox" )
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            trackBar1.Value = CvtFunc.ToInt(textBox1.Text);
            trackBar2.Value = CvtFunc.ToInt(textBox2.Text);   
            dbcon.CloseCon();
        }

        private bool UpdateData(string strExamSeqNo)
        {
            if (!VaildCheck()) return false;
            string strTableName = mstrTableName  ;
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox" )
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString(), DateFormatOption.DateFmtYMD);
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.MeniereDisease + "'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.MeniereDisease;
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateData()) this.Close(); 
        }

        private bool  UpdateData()
        {   
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;
            string strCaseName = string.Empty;
            dbcon.BeginTran();
            string strRemark = string.Empty;
            strSQL = "select top 1 * from mTreatHistory where SickRecID='" + mstrSickRecID + "' order by RecID desc";
            DataRow drCaseName = dbcon.GetDataFirstRow(strSQL);
            if (drCaseName != null)
            {
                strCaseName = CvtFunc.ToString(drCaseName["TreatCaseName"]);
            }

            if (SysVar.CurrentMenuInfo.strMenuName.Trim() == "疗效评估")
                strRemark = "";
            else
                strRemark = SysVar.CurrentMenuInfo.strMenuName.Trim();

            strCaseName = DataFunctions.GetFieldValue(dbcon, "mTreatHistory", "TreatCaseName + ' ' +Thrombolysis", " recid in ( select max(recid) from mTreatHistory) and sickRecID='"+ mstrSickRecID +"'");
            strSQL = "if not exists ( select * from TreatmentEffectHdr_Emergency where Type='" + TitleType.MeniereDisease + "' and SickRecID='" + mstrSickRecID + "' and SeqNo='" + mstrSeqNo + "')";
            strSQL += " Insert into TreatmentEffectHdr_Emergency(  SickRecID, SeqNo, ExamDate,Remark,Type,CaseName ) " +
                         " values (  '" + mstrSickRecID + "','" + mstrSeqNo + "','" + BaseFunctions.FormatDate(DateTime.Now.Date.ToShortDateString() ,  DateFormatOption.DateFmtYMD) + "' " +
                         " ,'" + strRemark + "','" + TitleType.MeniereDisease + "','" + strCaseName + "' ) ";
            dbcon.ExecuteSql(strSQL);

            if (!UpdateData(mstrSeqNo))
            {
                dbcon.RollbackTran(); 
                return false ;
            }
            dbcon.CommitTran(); 
            mfrmStatus = FormStatus.Update;
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                UpdateData();
                this.Close();
            }
            else 
                this.Close();
        }

        private void frmMDTreatmentEffectTitle_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }

        private void panel22_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel20_Paint(object sender, PaintEventArgs e)
        {

        }


        private bool VaildCheck()
        {
            //foreach (Control con in marrlist)
            //{
            //    if (con.Name.IndexOf("pnl_") >= 0)
            //    {
            //        foreach (Control conDtl in con.Controls)
            //        {
            //            if (conDtl.Text == "")
            //            {
            //                MessageBox.Show("必须填写 听力评定。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //                conDtl.Focus();
            //                return false;
            //            }
            //        }
            //    }
            //}
            return true;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            textBox1.Text = trackBar1.Value.ToString ();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            textBox2.Text = trackBar2.Value.ToString (); 
        }

      

        private void CalScore()
        {
            double dblTot = 0;
            dblTot = CvtFunc.ToInt(textBox3.Text);
            dblTot += CvtFunc.ToInt(textBox4.Text) * 2;
            dblTot += CvtFunc.ToInt(textBox5.Text) * 3;
            dblTot += CvtFunc.ToInt(textBox6.Text) * 4;
            textBox7.Text = dblTot.ToString();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            CalScore();
        }
    }
}
